package com.ruoyi.project.mobile.user;

import com.ruoyi.common.utils.R;
import com.ruoyi.common.utils.RFactory;
import com.ruoyi.project.system.address.domain.MemberAddress;
import com.ruoyi.project.system.address.service.IMemberAddressService;
import com.ruoyi.project.system.order.order.domain.OrderOrder;
import com.ruoyi.project.system.order.order.dto.OrderOrderDto;
import com.ruoyi.project.system.order.order.service.IOrderOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 功能：
 *
 * @author：dell
 * @create：2020/6/22 17:37
 * @version：2020 Version：1.0
 * @company：老屋新房 Created with IntelliJ IDEA
 */
@RestController
@RequestMapping("/api/address")
public class ApiAddressController {

	@Autowired
	private IMemberAddressService memberAddressService;
	@Autowired
    private IOrderOrderService orderOrderService;

	/**
	 * 查询用户地址
	 * @param uid
	 * @return
	 */
	@GetMapping
	private R memeberAddress(@RequestParam String uid){
		MemberAddress memberAddress = new MemberAddress();
		memberAddress.setMemberId(uid);
		memberAddress.setStatus(0);
		return RFactory.generateR(this.memberAddressService.selectMemberAddressList(memberAddress));
	}

	/**
	 * 新增用户地址
	 * @param memberAddress
	 * @return
	 */
	@PostMapping
	@Transactional
	private R addAddress(@RequestBody MemberAddress memberAddress){
		if(StringUtils.isBlank(memberAddress.getId())){
			if(memberAddress.getIsDefault()==0){
				//取消原本的默认地址
				this.memberAddressService.removeDefault(memberAddress.getMemberId());
			}
			memberAddress.setStatus(0);
			this.memberAddressService.insertMemberAddress(memberAddress);
		}else{
			this.memberAddressService.updateMemberAddress(memberAddress);
		}
		return R.ok();
	}

	/**
	 * 删除地址
	 * @return
	 */
	@DeleteMapping
	private R deleteAddress(@RequestBody Map<String,Object> map){
		MemberAddress memberAddress = this.memberAddressService.selectMemberAddressById((String) map.get("id"));
		if(!memberAddress.getMemberId().equals((String) map.get("uid"))){
			return R.ok();
		}
		this.memberAddressService.deleteMemberAddressById((String) map.get("id"));
		return R.ok();
	}

	/**
	 * 修改默认地址
	 * @param map
	 * @return
	 */
	@PutMapping("/default")
	private R updateDefault(@RequestBody Map<String,Object> map){
		MemberAddress oldAddress = this.memberAddressService.selectMemberAddressById((String) map.get("id"));
		if(oldAddress==null||!oldAddress.getMemberId().equals((String) map.get("uid"))){
			return R.ok();
		}
		//取消原本的默认地址
		this.memberAddressService.removeDefault((String) map.get("uid"));
		MemberAddress memberAddress = new MemberAddress();
		memberAddress.setIsDefault(0);
		memberAddress.setId((String) map.get("id"));
		this.memberAddressService.updateMemberAddress(memberAddress);
		return R.ok();
	}

	/**
	 * 查询地址详情
	 * @param uid
	 * @param id
	 * @return
	 */
	@GetMapping("/info")
	private R addressInfo(@RequestParam String uid,@RequestParam String id){
		return RFactory.generateR(this.memberAddressService.selectMemberAddressById(id));
	}

	/**
	 * 修改用户地址信息
	 * @param memberAddress
	 * @return
	 */
	@PutMapping
	private R updateAddress(@RequestBody MemberAddress memberAddress){
		return RFactory.generateR(this.memberAddressService.updateMemberAddress(memberAddress));
	}

    /**
     * 查询待发货订单地址详情
     * @param uid
     * @param orderOn 订单号
     * @return
     */
    @GetMapping("/orderAddress")
    public R orderAddress(@RequestParam String uid,@RequestParam String orderOn){
        return RFactory.generateR(this.orderOrderService.findByOrderNo(orderOn));
    }

    /**
     * 修改订单收货地址
     * @param orderOrder
     * @return
     */
    @PostMapping("/updateOrderAddress")
    public R updateOrderAddress(@RequestBody OrderOrder orderOrder){
        OrderOrderDto byOrderNo = orderOrderService.findByOrderNo(orderOrder.getOrderNo());
        if (byOrderNo == null) {
            return R.error("订单异常");
        } else if (byOrderNo.getStatus() !=0 && byOrderNo.getStatus() != 1) {
            return R.error("订单已发货，无法更改订单地址");
        }
//        byOrderNo.setId(byOrderNo.getId());
        byOrderNo.setConsigneeName(orderOrder.getConsigneeName());
        byOrderNo.setConsigneeTel(orderOrder.getConsigneeTel());
        byOrderNo.setArea(orderOrder.getArea());
        byOrderNo.setAddress(orderOrder.getAddress());
        this.orderOrderService.updateOrderOrder(byOrderNo);
        return R.ok();
    }

}
